package com.myk.game.shapechesstool.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.EditText;


/**
 * 动态获取用户输入后在继续后面的流程
 * 这是一个模态阻塞对话框（阻塞主线程，结果不用回调来处理）
 */
public class BlockingInputDialog {
    String mInputString = "";
    Activity mContext;
    String mTitle;
    EditText mEditText;
    Handler mHandler;
    int minLen;
    int maxLen;

    public BlockingInputDialog(Activity context, String title){
        mContext = context;
        mTitle = title;
    }

    public BlockingInputDialog(Activity context, String title,String inputString){
        mContext = context;
        mTitle = title;
        mInputString = inputString;
    }

    public String showDialog(){
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                //super.handleMessage(msg);
                throw new RuntimeException();
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(mTitle);
        builder.setCancelable(false);
        mEditText = new EditText(mContext);
        builder.setView(mEditText);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mInputString = mEditText.getText().toString();
                Message message = mHandler.obtainMessage();
                mHandler.sendMessage(message);
            }
        });
        mEditText.setText(mInputString);
        builder.setNegativeButton("取消", null);
        builder.create().show();
        try {
            Looper.getMainLooper().loop();
        }
        catch(RuntimeException e2)
        {
        }
        return mInputString;
    }
}